<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="18" :xs="24">
        <el-card>
          <div slot="header" class="clearfix">
            <span>基本资料</span>
          </div>
          <el-form ref="form" :model="club" label-width="80px">
            <el-form-item label="社团头像" prop="avatar" :rules="[{ required: true, message: '请上传社团头像', trigger: 'blur' }]">
              <el-upload
                class="avatar-uploader"
                action="http://localhost:8080/master/club/avatar"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
                <img v-if="club.avatar" :src="club.avatar" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label="社团名称" prop="name" :rules="[{ required: true, message: '社团名称不能为空', trigger: 'blur' }]" >
              <el-input v-model="club.name" />
            </el-form-item>
            <el-form-item label="社团口号" prop="slogan" :rules="[{ required: true, message: '社团介绍尽量不能为空', trigger: 'blur' }]" >
              <el-input v-model="club.slogan" />
            </el-form-item>
            <el-form-item label="社团介绍" prop="instruction" :rules="[{ required: true, message: '社团介绍不能为空', trigger: 'blur' }]">
              <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入社团介绍"
                v-model="club.instruction">
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" size="mini" @click="submit" v-hasPermi="['master:club:edit']">保存</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

<script>
import { getUserProfile } from "@/api/system/user";
import { updateClub } from "@/api/master/club";


export default {
  name: "Profile",
  data() {
    return {
      user: {},
      roleGroup: {},
      postGroup: {},
      activeTab: "userinfo",
      club: {}
    };
  },
  created() {
    this.getUser();
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.club.avatar = process.env.VUE_APP_BASE_API + res.imgUrl;
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isLt2M;
    },
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateClub(this.club).then(response => {
              this.msgSuccess("修改成功");
            });
        }
      });
    },
    getUser() {
      getUserProfile().then(response => {
        this.user = response.data;
        this.roleGroup = response.roleGroup;
        this.postGroup = response.postGroup;
        this.club = response.club;
      });
    }
  }
};
</script>
